import React, { useEffect } from 'react'
import { Tree } from 'antd'
import { connect } from 'react-redux'
import { useNavigate } from 'react-router'
import './index.less'
import { useDispatch } from 'react-redux'
import { setBookAll, setBorrowingRecord, setMyBookFriends } from '../../action/action'
import { getUInfo } from '../../../session/session'

const treeData = [
  {
    key: 'bookInfo',
    dataIndex: 'bookInfo',
    title: '图书信息',
  },
  {
    key: 'BorrowingRecord',
    dataIndex: 'BorrowingRecord',
    title: '借阅记录',
  },
  {
    key: 'MyBookFriends',
    dataIndex: 'MyBookFriends',
    title: '我的书友',
  },
  {
    key: 'UserInfo',
    dataIndex: 'UserInfo',
    title: '个人信息',
  }
]

const SiderContent = (props) => {
  const navigate = useNavigate()
  const dispatch = useDispatch()

  useEffect(() => {
    navigate('bookInfo')
    const param = { pageNo: '1', pageSize: '100000' }
    dispatch(setBookAll(param))
  }, [])

  function clickItem (selectedKeys, e) {
    const info = JSON.parse(getUInfo())
    const readerId = info.readerId.toString()
    navigate(selectedKeys[0])
    let param = {}
    let url = {}
    switch (selectedKeys[0]) {
      case 'MyBookFriends':
        param = { uid: readerId }
        dispatch(setMyBookFriends(param))
        break
      case 'BorrowingRecord':
        // 这里的id需要修改
        param = { id: readerId, pageNo: 1, pageSize: 10000 }
        dispatch(setBorrowingRecord(param))
        break
      default:
        param = { pageNo: '1', pageSize: '10000' }
        dispatch(setBookAll(param))
        break
    }
  }

  return (
    <Tree
      onSelect={clickItem}
      defaultSelectedKeys={['bookInfo']}
      style={{
        cursor: 'pointer',
        backgroundColor: '#1f2751'
      }}
      treeData={treeData}
    />
  )
}

const mapStateToProps = (state) => {
  return {}
}
export default connect(mapStateToProps)(SiderContent)